import { defineStore } from 'pinia'
import {
  IBillBook, IBillCategory,
  IBillPay, IBillRecord, IBillType
} from '@/types/bill'
import {
  addBillRecordApi, deleteBillRecordApi,
  queryBillBookApi,
  queryBillCategoryApi, queryBillPayApi,
  queryBillRecordApi,
  queryBillStatsApi, queryBillSummaryApi, updateBillRecordApi
} from '@/apis/bill'
import { getCurrentMonth, getCurrentYear, getStartEndDate } from 'vue3-common/utils/dateUtil'
import { IDateType, ImagePathEnum } from '@/types'
import { IDialog, IStatsChart, IHandleApi } from 'vue3-common/types'
import { ElMessage } from 'element-plus'
import { isMobile } from 'vue3-common/utils/layoutUtil'

export const useBillStore = defineStore('bill', {
  state: () => ({
    billRecordList: [] as IBillRecord[],
    currentBillRecord: {
      id: 0,
      bookName: '',
      type: '',
      category: '',
      date: '',
      location: '',
      payAccount: '',
      amount: 0,
      content: '',
      remark: '',
      imageList: []
    } as IBillRecord,
    billPayList: [] as IBillPay[],
    billBookList: [] as IBillBook[],
    billCategoryList: [] as IBillCategory[],
    billType: 'expensive' as IBillType,
    billDateType: 'month' as IDateType,
    billDateList: [] as string[],
    billImagePath: ImagePathEnum.BILL,
    billQueryForm: {
      billBook: 'all',
      billYear: getCurrentYear(),
      billMonth: getCurrentMonth()
    },
    billDateStatsList: [] as IStatsChart[],
    billBookStatsList: [] as IStatsChart[],
    billCategoryStatsList: [] as IStatsChart[],
    billApiType: 'ADD' as IHandleApi,
    billDialog: {
      title: '',
      visible: false,
      data: {}
    } as IDialog<IBillRecord>,
    isRefresh: false,
    isLoading: false
  }),
  actions: {
    async initBillInfo() {
      await this.queryBillPay()
      await this.queryBillBook()
      await this.queryBillCategory()
    },
    async queryBillPay() {
      this.billPayList = await queryBillPayApi()
    },
    async queryBillBook() {
      this.billBookList = await queryBillBookApi()
    },
    async queryBillCategory() {
      this.billCategoryList = await queryBillCategoryApi()
    },
    async queryBillRecord(id: number) {
      this.currentBillRecord = await queryBillRecordApi(id)
    },
    async queryBillSummary(bookName: string) {
      this.billRecordList = await queryBillSummaryApi({
        bookName,
        type: '',
        startDate: this.billDateList[0],
        endDate: this.billDateList[1]
      })
    },
    async queryBillStats(bookName: string) {
      this.billDateStatsList = await queryBillStatsApi(this.billDateType, {
        bookName,
        type: this.billType,
        startDate: this.billDateList[0],
        endDate: this.billDateList[1]
      })
      this.billBookStatsList = await queryBillStatsApi('book', {
        bookName: '',
        type: this.billType,
        startDate: this.billDateList[0],
        endDate: this.billDateList[1]
      })
      this.billCategoryStatsList = await queryBillStatsApi('category', {
        bookName,
        type: this.billType,
        startDate: this.billDateList[0],
        endDate: this.billDateList[1]
      })
    },
    async handleBillApi(billRecord: IBillRecord) {
      switch (this.billApiType) {
        case 'ADD':
          await addBillRecordApi(billRecord)
          ElMessage.success('新增账单成功')
          break
        case 'UPDATE':
          await updateBillRecordApi(billRecord.id as number, billRecord)
          ElMessage.success('更新账单成功')
          break
        case 'DELETE':
          await deleteBillRecordApi(billRecord.id as number)
          ElMessage.success('删除账单成功')
          break
        default:
          break
      }
      this.billDialog.visible = false

      if (!isMobile()) {
        await this.refreshInfo()
      }
    },
    async refreshInfo() {
      this.isLoading = true
      const bookName = this.billQueryForm.billBook === 'all' ? '' : this.billQueryForm.billBook
      const { billYear, billMonth } = this.billQueryForm
      const date = this.billDateType === 'year' ? billYear : billMonth
      this.billDateList = getStartEndDate(date, this.billDateType)

      await this.queryBillSummary(bookName)
      await this.queryBillStats(bookName)
      this.isRefresh = !this.isRefresh
      this.isLoading = false
    },
    getEmptyBillRecord(): IBillRecord {
      return {
        amount: 0,
        bookName: '',
        category: '',
        content: '',
        date: '',
        location: '',
        payAccount: '',
        remark: '',
        imageList: [],
        type: ''
      }
    }
  }
})
